package com.cskaoyan.service.mall;

import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketIssue;
import com.cskaoyan.bean.po.MarketIssueExample;
import com.cskaoyan.mapper.MarketIssueMapper;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MallIssueServicelmpl implements MallIssueService {

    @Autowired
    MarketIssueMapper marketIssueMapper;

    @Override
    public CommonData<MarketIssue> queryMallIssueList(BasePageInfo basePageInfo, String question) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        MarketIssueExample marketIssueExample = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = marketIssueExample.createCriteria();
        if (!StringUtil.isEmpty(question)) {
            criteria.andQuestionLike("%" + question.trim() + "%");
        }
        marketIssueExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        List<MarketIssue> marketIssues = marketIssueMapper.selectByExample(marketIssueExample);

        PageInfo<MarketIssue> marketIssuePageInfo = new PageInfo<>(marketIssues);

        return CommonData.data(marketIssuePageInfo);
    }

    @Override
    public MarketIssue createMallIssue(MarketIssue marketIssue) {
        marketIssue.setAddTime(new Date());
        marketIssue.setUpdateTime(new Date());
        int code = marketIssueMapper.insertSelective(marketIssue);
        if (code == 0) {
            return null;
        }
        MarketIssue marketIssue1 = marketIssueMapper.selectByPrimaryKey(marketIssue.getId());
        return marketIssue1;
    }

    @Override
    public MarketIssue updateMallIssue(MarketIssue marketIssue) {
        marketIssue.setUpdateTime(new Date());
        int code = marketIssueMapper.updateByPrimaryKeySelective(marketIssue);
        if (code == 0) {
            return null;
        }
        MarketIssue marketIssue1 = marketIssueMapper.selectByPrimaryKey(marketIssue.getId());
        return marketIssue1;
    }

    @Override
    public int deleteMallIssue(Integer id) {
        int code = marketIssueMapper.deleteByPrimaryKey(id);
        return code;
    }
}
